package com.foreverwin.jzymes.common.service.impl;

import com.foreverwin.jzymes.common.mapper.SfcStepAdviceMapper;
import com.foreverwin.jzymes.common.service.ISfcStepAdviceService;
import com.foreverwin.jzymes.framework.util.I18nUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.List;
import java.util.Map;

@Service
public class SfcStepAdviceServiceImpl implements ISfcStepAdviceService {

    @Autowired
    SfcStepAdviceMapper sfcStepAdviceMapper;

    /**
     * 查询SfcRoutingBo
     *
     * @param sfcBo
     * @return
     */
    public String findSfcRoutingBo( String sfcBo ){
        Assert.hasText( sfcBo, I18nUtil.getI18nText( "common.sfcBoNotBlank" ) );
        String sfcRoutingBo = sfcStepAdviceMapper.selectSfcRoutingBo( sfcBo );
        Assert.hasText( sfcRoutingBo, I18nUtil.getI18nText( "common.sfcRoutingBoNotExist", new String[]{ sfcBo } ) );
        return sfcStepAdviceMapper.selectSfcRoutingBo( sfcBo );
    }

    /**
     * 查询SfcRouterMap
     *
     * @param sfcRoutingBo
     * @return
     */
    public Map<String,Object> findSfcRouterMap( String sfcRoutingBo ){
        Assert.hasText( sfcRoutingBo, I18nUtil.getI18nText( "common.sfcRoutingBoNotBlank" ) );
        return sfcStepAdviceMapper.selectSfcRouterMap( sfcRoutingBo );
    }

    /**
     * 查询SfcRouterBo
     *
     * @param sfcBo
     * @return
     */
    public String findSfcRouterBo( String sfcBo ){
        String sfcRoutingBo = findSfcRoutingBo( sfcBo );
        Assert.hasText( sfcRoutingBo, I18nUtil.getI18nText("common.sfcRoutingBoNotExist", new String[]{ sfcBo } ) );
        Map<String,Object> sfcRouterMap = findSfcRouterMap( sfcRoutingBo );
        return (String) sfcRouterMap.get( "HANDLE" );
    }

    /**
     * 查询SfcStep列表
     *
     * @param sfcBo
     * @return
     */
    public List<Map<String,Object>> findSfcStepList( String sfcBo ){
        String sfcRouterBo = findSfcRouterBo( sfcBo );
        return  sfcStepAdviceMapper.selectSfcStepList( sfcRouterBo );
    }

    /**
     * 查询Sfc第一个工序
     *
     * @param sfcBo
     * @return
     */
    public String findSfcFirstOperation( String sfcBo ){
        List<Map<String,Object>> sfcStepList = findSfcStepList( sfcBo );
        if( sfcStepList != null && sfcStepList.size() > 0 ){
            Map<String,Object> sfcStepMap = sfcStepList.get( 0 );
            return (String) sfcStepMap.get( "OPERATION_BO" );
        }
        return null;
    }
}
